import socket
import json
import tqdm
port=2246

def server_create():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('127.0.0.1', port))
    sock.listen(5)
    return sock

def client_create():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('127.0.0.1', port))
    return sock

'''
将客户端数据打包成一个字典
'''
def get_request_dic(mode,**kwargs):
    if mode=='register':
        data={
            'mode':'register',
            'user_name':kwargs.get('user_name'),
            'user_num':kwargs.get('user_num'),
            'user_password':kwargs.get('user_password'),
            'time':kwargs.get('time'),
        }
    elif mode=='login':
        data={
            'mode':'login',
            'user_num':kwargs.get('user_num'),
            'user_password':kwargs.get('user_password'),
            'time':kwargs.get('time'),
        }
    elif mode=='talk':
        data={
            'mode':'talk',
            'user_name':kwargs.get('user_name'),
            'msg':kwargs.get('msg'),
            'time':kwargs.get('time'),
        }
    elif mode=='file':
        data={
            'mode':'file',
            'file_name':kwargs.get('file_name'),
            'file_size':kwargs.get('file_size'),
        }
    elif mode=='private_message':
        data={
            'mode':'private_message',
            'user1':kwargs.get('user1'),
            'user2':kwargs.get('user2'),
            'msg':kwargs.get('msg'),
            'time':kwargs.get('time'),
        }
    elif mode=='contacts':
        data={
            'mode':'contacts',
            'user_num':kwargs.get('user_num'),
        }
    elif mode=='add_contact':
        data={
            'mode':'add_contact',
            'user_num':kwargs.get('user_num'),
            'contact_num':kwargs.get('contact_num'),
            'time':kwargs.get('time'),
        }
    else:
        data={
            'mode':'exit',
            'user_name':kwargs.get('user_name'),
            'time':kwargs.get('time'),
        }
    return data
'''
将服务端数据打包成一个字典
'''
def get_response_dic(**kwargs):
    data={
        'code':kwargs.get('code'),
        'mode':kwargs.get('mode'),
        'user_name':kwargs.get('user_name'),
        'msg':kwargs.get('msg'),
        'time':kwargs.get('time'),
    }
    return data


'''
将数据转换位字节并发出
'''
def send_mes(socket,data):
    data_str=json.dumps(data)
    data_str_len=len(data_str.encode('utf-8')).to_bytes(8, byteorder='big')
    socket.send(data_str_len)
    socket.send(data_str.encode('utf-8'))
'''
接收数据并将其转换为字典
'''
def recv_mes(socket):
    byte_len = socket.recv(8)
    if not byte_len:
        return None
    stream_len=int.from_bytes(byte_len, byteorder='big')
    dic_byte=bytes()
    while stream_len>0:
        if stream_len>4096:
            temp=socket.recv(stream_len)
        else:
            temp=socket.recv(4096)
        if not temp:
            break
        dic_byte+=temp
        stream_len-=len(temp)
    dic=json.loads(dic_byte.decode('utf-8'))
    return dic


'''
判断账号是否已经被登入
'''
def is_num(num,lis):
    for i in lis :
        if num==i['user_num']:
            return True
    return False


'''
创建进度条
'''
def create_tqdm(size,file_name):
   tqdm_=tqdm.tqdm(
       total=size,
       desc=f"发送 {file_name}"
   )
   return tqdm_




